/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.editor.figures;

import org.eclipse.draw2d.ConnectionEndpointLocator;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.swt.graphics.Color;

import ro.pub.cs.se.moondance.model.Connection;

public class ConnectionFigure extends PolylineConnection {

	@SuppressWarnings("unused")
	private Integer type;
	@SuppressWarnings("unused")
	private Integer lineStyle;

	Color color;

	PolygonDecoration decoration = new PolygonDecoration();
	PointList decorationPointList = new PointList();

	EditableLabel nameLabel, sourceMultiplicityLabel, targetMultiplicityLabel;

	ConnectionEndpointLocator sourceEndpointLocator, targetEndpointLocator,
			relationshipLocator;

	public ConnectionFigure(Integer type, Integer lineStyle) {
		super();
		this.type = type;
		this.lineStyle = lineStyle;

		if (type.equals(Connection.ASSOCIATION)) {
			nameLabel = new EditableLabel(Connection.ASSOCIATION_NAME,
					EditableLabel.ASSOCIATION_NAME);
		} else

		if (type.equals(Connection.DIRECTED_ASSOCIATION)) {
			nameLabel = new EditableLabel(Connection.DIRECTED_ASSOCIATION_NAME,
					EditableLabel.ASSOCIATION_NAME);
			decorationPointList.addPoint(0, 0);
			decorationPointList.addPoint(-2, -2);
			decorationPointList.addPoint(-2, 2);

			color = new Color(null, 0, 0, 0);
		} else

		if (type.equals(Connection.AGGREGATION)) {
			decorationPointList.addPoint(0, 0);
			decorationPointList.addPoint(-2, 2);
			decorationPointList.addPoint(-4, 0);
			decorationPointList.addPoint(-2, -2);

			color = new Color(null, 255, 255, 255);
			nameLabel = new EditableLabel(Connection.AGGREGATION_NAME,
					EditableLabel.ASSOCIATION_NAME);
			targetMultiplicityLabel = new EditableLabel("1",
					EditableLabel.MULTIPLICITY_NAME);
			sourceMultiplicityLabel = new EditableLabel("1..*",
					EditableLabel.MULTIPLICITY_NAME);
		} else

		if (type.equals(Connection.COMPOSITION)) {
			decorationPointList.addPoint(0, 0);
			decorationPointList.addPoint(-2, 2);
			decorationPointList.addPoint(-4, 0);
			decorationPointList.addPoint(-2, -2);

			color = new Color(null, 0, 0, 0);
			nameLabel = new EditableLabel(Connection.COMPOSITION_NAME,
					EditableLabel.ASSOCIATION_NAME);
			targetMultiplicityLabel = new EditableLabel("1",
					EditableLabel.MULTIPLICITY_NAME);
			sourceMultiplicityLabel = new EditableLabel("1..*",
					EditableLabel.MULTIPLICITY_NAME);
		} else

		if (type.equals(Connection.INHERITANCE)) {
			decorationPointList.addPoint(0, 0);
			decorationPointList.addPoint(-2, 2);
			decorationPointList.addPoint(-2, -2);

			color = new Color(null, 255, 255, 255);
			nameLabel = new EditableLabel(Connection.INHERITANCE_NAME,
					EditableLabel.ASSOCIATION_NAME);
		} else

		if (type.equals(Connection.REALIZATION)) {
			decorationPointList.addPoint(0, 0);
			decorationPointList.addPoint(-2, 2);
			decorationPointList.addPoint(-2, -2);

			color = new Color(null, 255, 255, 255);
			nameLabel = new EditableLabel(Connection.REALIZATION_NAME,
					EditableLabel.ASSOCIATION_NAME);
		}

		if (color != null)
			decoration.setBackgroundColor(color);

		decoration.setTemplate(decorationPointList);
		setLineStyle(lineStyle);
		setTargetDecoration(decoration);

		/* Adding labels to the connection */
		targetEndpointLocator = new ConnectionEndpointLocator(this, true);
		targetEndpointLocator.setVDistance(15);

		if (targetMultiplicityLabel != null)
			add(targetMultiplicityLabel, targetEndpointLocator);

		sourceEndpointLocator = new ConnectionEndpointLocator(this, false);
		sourceEndpointLocator.setVDistance(15);

		if (sourceMultiplicityLabel != null)
			add(sourceMultiplicityLabel, sourceEndpointLocator);

		relationshipLocator = new ConnectionEndpointLocator(this, true);
		relationshipLocator.setUDistance(10);
		relationshipLocator.setVDistance(-20);

		add(nameLabel, relationshipLocator);
	}

	public EditableLabel getNameLabel() {
		return nameLabel;
	}

	public EditableLabel getSourceMultiplicityLabel() {
		return sourceMultiplicityLabel;
	}

	public EditableLabel getTargetMultiplicityLabel() {
		return targetMultiplicityLabel;
	}

}
